home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-06-15 | 3.1 KB | 145 lines | [TEXT/MMCC] |
- // ===========================================================================
- // UScripting.cp -- support class for scripting
- // ===========================================================================
- // © 1995 James Kaput, Jeremy Roschelle SimCalc Project
-
- #include "UScripting.h"
-
- ComponentInstance UScripting::sComponent = nil;
-
- void
- UScripting::Initialize()
- {
- if (! sComponent)
- SetComponent(::OpenDefaultComponent(kOSAComponentType,'scpt'));
- }
-
- OSErr
- UScripting::ParseScriptFile(FSSpec &inFile, Handle &outScript, Handle &outText)
- {
- short fRefNum = -1;
- OSErr result = noErr;
-
- Try_ {
- // open resource fork
- fRefNum = ::FSpOpenResFile(&inFile,fsRdPerm);
- ThrowIfResError_();
-
- // get the first script resource in the file
- outScript = ::Get1IndResource('scpt',1);
- FailNIL_(outScript);
- ::DetachResource(outScript); // detach resource lets us "own" this handle
-
- // the first text resource has the description of the script (if any)
- // we fail quietly if there's no text, since its optional
- outText = ::Get1IndResource('TEXT',1);
- if (outText) ::DetachResource(outText);
- }
-
- Catch_(catchErr) {
- result = catchErr;
- }
- EndCatch_
-
- if (fRefNum != -1) ::CloseResFile(fRefNum);
- return result;
- }
-
- OSErr
- UScripting::CreateScriptFile(FSSpec &inFile,Handle inScript, Handle inText)
- {
- OSErr err = noErr;
- Int16 fRefNum = -1;
- Try_ {
- // make the file
- ::FSpCreateResFile(&inFile,'ToyS',kOSAFileType,0);
- ThrowIfResError_();
-
- // open it
- fRefNum = ::FSpOpenResFile(&inFile,fsRdWrPerm);
- ThrowIfResError_();
-
- // copy the script handle and add it to the file
- Handle data = inScript;
- err = ::HandToHand(&data);
- ThrowIfOSErr_(err);
- ::AddResource(data,'scpt',128,nil);
- ThrowIfResError_();
-
- // copy the text and add it to the file
- if (inText) {
- data = inText;
- err = ::HandToHand(&data);
- ThrowIfOSErr_(err);
- ::AddResource(data,'TEXT',1128,nil);
- ThrowIfResError_();
- }
-
- // close (also saves) the resource file
- ::CloseResFile(fRefNum);
- ThrowIfResError_();
- }
- Catch_(catchErr) {
- err = catchErr;
- if (fRefNum != -1) ::CloseResFile(fRefNum);
- }
- EndCatch_
-
- return err;
- }
-
- OSErr
- UScripting::LoadScript(Handle inScript, OSAID &outScriptID)
- {
- Initialize();
-
- AEDesc scriptDesc;
- scriptDesc.descriptorType = typeOSAGenericStorage;
- scriptDesc.dataHandle = inScript;
- return ::OSALoad(sComponent,&scriptDesc,kOSAModeNull,&outScriptID);
- }
-
-
- OSErr
- UScripting::LoadScript(AEDesc &inScriptDesc, OSAID &outScriptID)
- {
- Initialize();
- return ::OSALoad(sComponent,&inScriptDesc,kOSAModeNull,&outScriptID);
- }
-
-
- OSErr
- UScripting::ExecuteScript(OSAID inScriptID)
- {
- Initialize();
- OSAID resultID;
- OSErr err;
-
- err = ::OSAExecute(sComponent,inScriptID,kOSANullScript,kOSAModeNull,&resultID);
- if (err) return err;
- else ::OSADispose(sComponent,resultID);
-
- return noErr;
- }
-
- OSErr
- UScripting::DisposeScript(OSAID inScriptID)
- {
- Initialize();
- return ::OSADispose(sComponent,inScriptID);
- }
-
-
- URun1Script::URun1Script(OSAID inScriptID)
- : mScriptID(inScriptID)
- {
- StartRepeating();
- }
-
- void
- URun1Script::SpendTime(const EventRecord &inMacEvent)
- {
- UScripting::ExecuteScript(mScriptID);
- delete this;
- }
-